package com.vim.modules.web.controller;

import java.util.List;
import javax.annotation.Resource;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.model.response.Response;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.model.response.TableListResponse;
import com.vim.common.utils.ValidateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.vim.modules.web.service.ShopOrderService;
import com.vim.modules.web.model.ShopOrder;

@RestController
@RequestMapping("/shopOrder")
public class ShopOrderController {

	@Resource
	private ShopOrderService shopOrderService;

	/**
	 * 列表查询
     * @param page 页码
     * @param limit 每页显示数量
	 */
	@RequestMapping("/list")
    public TableListResponse list(ShopOrder shopOrder, @RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotZero(page);
        ValidateUtils.checkNotZero(limit);
	    PageHelper.startPage(page,limit);
        List<ShopOrder> list = shopOrderService.findList(shopOrder);
        PageInfo<ShopOrder> pageInfo = new PageInfo<>(list);
        return new TableListResponse(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestfulResponse edit(ShopOrder shopOrder){
        if(StringUtils.isNoneBlank(shopOrder.getOrderId())){
            shopOrderService.update(shopOrder);
        }else{
            shopOrderService.save(shopOrder);
        }
        return new RestfulResponse(Response.OK);
    }

    /**
     * 详情
     * @param id 主键ID
     */
    @RequestMapping("/detail")
    public RestfulResponse detail(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        return new RestfulResponse(shopOrderService.find(id));
    }

    /**
     * 删除
     * @param id 主键ID
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public RestfulResponse del(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        shopOrderService.delete(id);
        return new RestfulResponse(Response.OK);
    }

}

